package com.twt.survey.controller;

import com.twt.survey.common.BaseResponse;
import com.twt.survey.common.ErrorCode;
import com.twt.survey.exception.BusinessException;
import com.twt.survey.pojo.Ans;
import com.twt.survey.pojo.Questionnaire;
import com.twt.survey.service.SurveyService;
import com.twt.survey.utils.ResultUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class SurveyController {

    @Resource
    private SurveyService service;

    @PostMapping("/survey")
    public BaseResponse<Integer> postSurvey(@RequestBody Ans ans) {
        if (ans == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数不能为空");
        }

        if (ans.getA_4().contains("群众") || ans.getA_4().contains("共青团员")){
            if (ans.getA_5() == null){
                return ResultUtils.fail(ErrorCode.UNFINISHED_STATUS, "尚未完成填写");
            } else {
                if (ans.getA_5().contains("都不是")){
                    if (ans.getA_6() == null){
                        return ResultUtils.fail(ErrorCode.UNFINISHED_STATUS, "尚未完成填写");
                    }
                }
            }
        }

        Questionnaire questionnaire = new Questionnaire(ans);
        service.insertSure(questionnaire);
        return ResultUtils.success(1);
    }
}
